/* $Id: TeamsTable.java 110 2009-05-28 20:57:15Z henk $ */
package org.jugger.tournament.gui.view;

import java.util.ArrayList;
import java.util.Collection;

import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

import org.jugger.tournament.model.Team;

import util.I18N;

public class TeamsTable extends JTable {

	private static final long serialVersionUID = -7100407046444632661L;

	public enum TeamsColumn {
		POSITION, NAME, CITY, IS_ADDITIONAL_TEAM
	}

	public TeamsTable(Collection<Team> teams) {
		reloadData(teams);
	}




	class TeamsTableModel extends AbstractTableModel {

		private static final long serialVersionUID = 5685959044888903092L;

		private String[] columnNames = {
	    	"numberAbbr", "teamName", "teamCity", "teamIsAdditionalTeam"
	    };

		private ArrayList<Team> teams;

	    public TeamsTableModel(Collection<Team> teams) {
			this.teams = new ArrayList<Team>(teams);
	    }

	    public int getColumnCount() {
	        return columnNames.length;
	    }

	    public int getRowCount() {
	        return teams.size();
	    }

	    @Override
	    public String getColumnName(int col) {
	        return I18N.__(columnNames[col]);
	    }

	    public Object getValueAt(int row, int col) {
	        Team team = teams.get(row);

	        switch(TeamsColumn.values()[col]) {
	        case POSITION:
	        	return row+1;
	        case NAME:
	        	return team.getName();
	        case CITY:
	        	return team.getCity();
	        case IS_ADDITIONAL_TEAM:
	        	return team.getIsAdditionalTeam() ? I18N.__("true") : I18N.__("false");
	        }

	        throw new RuntimeException("Invalid teams table column.");
	    }

	    @Override
	    public Class<? extends Object> getColumnClass(int c) {
	        return getValueAt(0, c).getClass();
	    }
	}

	public void reloadData(Collection<Team> teams) {
		setModel(new TeamsTableModel(teams));
		getColumnModel().getColumn(TeamsColumn.POSITION.ordinal()).setMinWidth(35);
		getColumnModel().getColumn(TeamsColumn.POSITION.ordinal()).setMaxWidth(35);
	//	setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
	}
}
